package com.nuonuo.util.jar;


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @author duan
 * @version 1.0
 * @date 2023/12/15 17:09
 */
public class JarTests {

    public static void main(String[] args) throws IOException {
        String[] appNames = new String[] {
                "nuonuo-dzfp-manage",
                "nuonuo-dzfp-boxlogin",
                "nuonuo-dzfp-business",
                "nuonuo-dzfp-convert",
                "nuonuo-dzfp-data-process",
                "nuonuo-dzfp-invoice-check",
                "nuonuo-dzfp-invoice-split",
                "nuonuo-dzfp-middleware",
                "nuonuo-dzfp-order-center",
                "nuonuo-dzfp-proxy",
                "nuonuo-dzfp-scheduler",
                "nuonuo-dzfp-sharding-support",
        };
//        String fileDir = "F:\\tmp\\1221\\";
//        String fileDir = "F:\\tmp\\";
        String fileDir = "F:\\tmp\\1221-2\\";
        extractAll(fileDir, appNames, "1221-2", "2.0.0-SNAPSHOT");
//        extractAll(fileDir, appNames, "1221", "2.0.0-SNAPSHOT");
//        extractAll(fileDir, appNames, "jar", "3.23.7");
    }

    private static void extractAll(String fileDir, String[] appNames, String prefix, String version) throws IOException {
        for (String appName : appNames) {
            String jarPath = fileDir + appName + "-" + version + ".jar";
            String targetPath = fileDir + prefix + "-" + appName + ".txt";
            extractJar(jarPath, targetPath);
        }
    }


    public static void extractJar(String jarPath, String targetPath) throws IOException {
        JarFile jarFile = new JarFile(jarPath);
        Enumeration<JarEntry> entries  = jarFile.entries();

        FileWriter writer = new FileWriter(targetPath);
        BufferedWriter bw = new BufferedWriter(writer);
        String libPrefix = "BOOT-INF/lib/";
        //遍历整个jar文件
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            if (jarEntry.getName().startsWith(libPrefix)) {
                String jarName = jarEntry.getName().substring(libPrefix.length());
                if (jarName.endsWith("2.0.0-SNAPSHOT.jar")) {
                    int index = jarName.indexOf("2.0.0-SNAPSHOT");
                    int end = jarName.lastIndexOf(".");
                    String name = jarName.substring(0, index-1);
                    String version = jarName.substring(index, end);
                    bw.write(name + "," +  version + "\n");
                } else if (jarName.contains("-")) {
                    int index = jarName.lastIndexOf("-");
                    int end = jarName.lastIndexOf(".");
                    String name = jarName.substring(0, index);
                    String version = jarName.substring(index + 1, end);
                    bw.write(name + "," +  version + "\n");
                }
            } else if (jarEntry.getName().endsWith(".jar")) {
                bw.write(jarEntry.getName() + "\n");
            }
        }

        bw.close();
        writer.close();
    }
}
